﻿using AngleSharp.Dom;
using AngleSharp.Dom.Html;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Abot.Demo.CNBlogs
{
    public class CNBlogsNews
    {
        public string Title;
        public string Poster;
        public string PostDateTime;
        public string URL;
        public string Comment;
        public string Read;

        //{https://news.cnblogs.com/n/557524
        public void Parse(IHtmlDocument angleSharpHtmlDocument)
        { 
            Title = angleSharpHtmlDocument.QuerySelector("#news_title").FirstChild.TextContent; //selector
            Poster = angleSharpHtmlDocument.QuerySelector("#news_info > span.news_poster > a").FirstChild.TextContent; //selector

            //((\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}))
            PostDateTime = angleSharpHtmlDocument.QuerySelector("#news_info > span.time").FirstChild.TextContent; //selector

            //http://www.cnblogs.com/codefish/p/4993809.html ajax动态调用方式
            Comment = GetValue(angleSharpHtmlDocument, "#News_CommentCount_Head"); //selector
            Read = GetValue(angleSharpHtmlDocument, "#News_TotalView"); //selector
            URL = angleSharpHtmlDocument.QuerySelector("#news_title > a").Attributes[0].Value; //selector
            
        }

        private string GetValue(IHtmlDocument angleSharpHtmlDocument, string selector)
        {
            INode node = angleSharpHtmlDocument.QuerySelector("#News_TotalView").FirstChild; //selector
            if (node != null)
            {
                return node.TextContent;
            }

            return "";

        }

        public override string ToString()
        {
            return string.Format("{0}\t{1}\t{2}\t{3}\n", Title, Poster, PostDateTime, URL);
        }
    }
}
